package com.itheima.appstore.vm;

import com.google.gson.Gson;
import com.itheima.appstore.model.net.AppInfo;
import com.itheima.appstore.model.net.HomeInfo;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by itheima.
 */
public abstract class BaseCallBack implements Callback {
    private CommonPager pager;
    private static Gson gson = new Gson();
    public BaseCallBack(CommonPager pager) {
        this.pager = pager;
    }

    @Override
    public void onFailure(Call call, IOException e) {
        pager.isReadData = false;
        // 更新界面
        pager.runOnUiThread();
    }

    @Override
    public void onResponse(Call call, Response response) throws IOException {
        // 判断：状态码（200）
        // 判断：服务器回复信息内容
        if (response.code() == 200) {
            pager.isReadData = true;
            String jsonString = response.body().string();
            onSuccess(jsonString);
        } else {
            pager.isReadData = false;
        }

        // 更新界面
        pager.runOnUiThread();
    }

    /**
     * 处理成功代码
     * @param json
     */
    protected abstract void onSuccess(String json);
}
